/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package BLL;

import DAL.ConnectData;
import DTO.Category1;
import java.sql.ResultSet;
import java.util.ArrayList;

/**
 *
 * @author Vien-IT
 */
public class BCategory1 {
    
    private ConnectData condata;

    public BCategory1() {
        condata = new ConnectData();
    }
    
    public ArrayList<Category1> getAll(String sql)
    {
        ArrayList<Category1> categories1 = new ArrayList<Category1>();
        Category1 category1 = null;
        try {
            ResultSet rs = condata.executeQuery(sql);
            while(rs.next())
            {
                category1 = new Category1();
                category1.setCategory1Id(rs.getInt("category1Id"));
                category1.setName(rs.getString("name"));
                categories1.add(category1);
               
            }
             condata.close();
        } catch (Exception e) {
        }
        
        return categories1.isEmpty() ? null : categories1;
    }
    
    public ArrayList<Category1> getAll()
    {
        String sql = "select * from tblCategory1";
           return getAll(sql);
    }
    
    
    public Category1 getById(int category1Id)
    {
        String sql = "select * from tblCategory1 where category1Id = "+category1Id+"";
        ArrayList<Category1> categories1 = getAll(sql);
        return categories1 == null ? null : categories1.get(0);
    }
    public boolean insert(Category1 category1)
    {
        boolean check = false;
        String sql = "insert into tblCategory1 (name) values (N'"+category1.getName()+"')";
        int rs = -1;
        try {
            rs = condata.executeUpdate(sql);
            if(rs > 0)
                check = true;
        } catch (Exception e) {
        }
        return check ;
    }
    
}
